﻿using Dapper;
using System;
using System.Collections.Generic;
using Global = Tauren.Business.Entity.Global;
using Model = Tauren.Business.Entity.Model;
using System.Linq;
using Tauren.Business.Entity.Model;
using System.Text;
using Tauren.Business.Infrastructure.Globals;

namespace Tauren.Business.DLL.AccountInfo
{
    /// <summary>
    /// 账号基本信息
    /// </summary>
    public class AccountInfoBusiness : Base.BaseDLL, IAccountInfoBusiness
    {
        public AccountInfoBusiness(IConnectionDLLBase _dLLBase) : base(_dLLBase)
        {
        }

        /// <summary>
        /// 删除账号信息
        /// </summary>
        /// <param name="AccountId"></param>
        /// <returns></returns>
        public bool Delete(object AccountId) => dbConnection.Execute(sql: "DELETE FROM  AccoutInfo WHERE AutoId=@AutoId", param: new Model.AccoutInfo() { AutoId = (int)AccountId }) >= 0;

        /// <summary>
        /// 根据账号获取账户信息
        /// </summary>
        /// <param name="Account"></param>
        /// <returns></returns>
        public Model.AccoutInfo GetByAccount(string Account) => dbConnection.Query<Model.AccoutInfo>(sql: "SELECT autoId,account,'' as `password`,`name`,empid,`enable`,sequence,`language`,creator,modifier,creatdate,modifdate FROM accoutinfo WHERE account=@account and `enable`=1", param: new Model.AccoutInfo { Account = Account }).FirstOrDefault();

        /// <summary>
        /// 根据组织获取账号信息
        /// </summary>
        /// <param name="OrgId"></param>
        /// <returns></returns>
        public List<Model.AccoutInfo> GetByOrgs(string OrgId) => dbConnection.Query<Model.AccoutInfo>(sql: $"SELECT a.* FROM accoutinfo a LEFT JOIN employee e on e.empid=a.empid WHERE   a.`enable`=1 AND e.orgId in({OrgId})").ToList();
        /// <summary>
        /// 分页显示账号信息
        /// </summary>
        /// <param name="queryModel"></param>
        /// <param name="Total"></param>
        /// <returns></returns>
        public List<Model.PageAccountModel> Pages(Global.QueryModel queryModel, out long Total)
        {
            Total = 0;
            string where = "";
            if (!string.IsNullOrEmpty(queryModel.KeyWord))
            {
                where += $" AND (a.Account LIKE '%{queryModel.KeyWord}%' OR a.Name LIKE '%{queryModel.KeyWord}%' )";
            }
            if (string.IsNullOrEmpty(queryModel.SortField))
            {
                queryModel.SortField = "a.AutoId";
                queryModel.IsAsc = false;
            }
            if (queryModel.OrgId > 0)
            {
                where += $" AND  e.orgid ={queryModel.OrgId}  ";
            }
            queryModel.Page = queryModel.Page == 0 ? 1 : queryModel.Page;
            string asc = queryModel.IsAsc ? "ASC" : "DESC";
            StringBuilder sql = new StringBuilder();
            sql.Append(@$"SELECT  count(a.EMPID) as cnt FROM ACCOUTINFO a LEFT JOIN EMPLOYEE e ON e.EMPID=a.EMPID WHERE  a.autoid>0  {where};");
            sql.Append(@$"SELECT a.*,e.OrgId FROM ACCOUTINFO a LEFT JOIN EMPLOYEE e ON e.EMPID=a.EMPID WHERE a.autoid>0 {where}  ORDER BY {queryModel.SortField} {asc} LIMIT {(queryModel.Page - 1) * queryModel.PageSize},{ queryModel.PageSize} ;");
            using (var reader = dbConnection.QueryMultiple(sql.ToString()))
            {
                Total = reader.ReadFirstOrDefault<int>();
                List<Model.PageAccountModel> eployeeModels = reader.Read<Model.PageAccountModel>().ToList();
                long RowNo = ((queryModel.Page == 0 ? 1 : queryModel.Page) - 1) * queryModel.PageSize + 1;
                eployeeModels.ForEach(o =>
                {
                    o.RowNo = RowNo;
                    RowNo++;
                });
                return eployeeModels;
            }
        } 
    }
}
